package com.henu.xyh.fragmentofXYH;

import android.content.Intent;
import android.os.Message;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.os.Handler;
import android.widget.ListView;

import com.baidu.platform.comapi.map.N;
import com.henu.R;
import com.henu.adapter.Listnewsadapter;
import com.henu.adapter.ViewpageAdapter;
import com.henu.xyh.WebviewXYHActivity;
import com.henu.xyh.entity.Express;
import com.henu.xyh.entity.News;
import com.henu.xyh.entity.Reply;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * A simple {@link Fragment} subclass.from xyh
 */
public class Home extends Fragment {



    public Home() {
        // Required empty public constructor
    }
    private ListView listView;
    private Listnewsadapter nadapter;
    private ArrayList<News> newslist=new ArrayList<>();
    private ViewPager viewpage;
    private ArrayList<ImageView> pointlist = new ArrayList<>();
    private ArrayList<View> viewlist = new ArrayList<>();
    private ViewpageAdapter adapter;
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


//        圆点的配置
        View view=inflater.inflate(R.layout.fragment_fragment1,null);
        ImageView img1 = (ImageView) view.findViewById(R.id.vp_top_img1);
        ImageView img2 = (ImageView) view.findViewById(R.id.vp_top_img2);
        ImageView img3 = (ImageView) view.findViewById(R.id.vp_top_img3);
        ImageView img4 = (ImageView) view.findViewById(R.id.vp_top_img4);
        ImageView img5 = (ImageView) view.findViewById(R.id.vp_top_img5);
        //添加至数组pointlist中
        pointlist.add(img1);
        pointlist.add(img2);
        pointlist.add(img3);
        pointlist.add(img4);
        pointlist.add(img5);
        viewpage = (ViewPager) view.findViewById(R.id.vp1);
        adapter = new ViewpageAdapter();
        viewpage.setAdapter(adapter);

        //滑动页面的配置
        View v1 = getActivity().getLayoutInflater().inflate(R.layout.inflate_vp_top_item1, null);
        View v2 = getActivity().getLayoutInflater().inflate(R.layout.inflate_vp_top_item2, null);
        View v3 = getActivity().getLayoutInflater().inflate(R.layout.inflate_vp_top_item3, null);
        View v4 = getActivity().getLayoutInflater().inflate(R.layout.inflate_vp_top_item4, null);
        View v5 = getActivity().getLayoutInflater().inflate(R.layout.inflate_vp_top_item5, null);
        //添加指数组viewlist中
        viewlist.add(v1);
        viewlist.add(v2);
        viewlist.add(v3);
        viewlist.add(v4);
        viewlist.add(v5);
        adapter.addData(viewlist);
        adapter.notifyDataSetChanged();
        //新闻列表
        listView= (ListView) view.findViewById(R.id.listview_news);
        nadapter = new Listnewsadapter(getActivity());
        listView.setAdapter(nadapter);
        final BmobQuery<News> query = new BmobQuery<News>();
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        query.order("-Publishdate");
        query.findObjects(new FindListener<News>() {
            @Override
            public void done(List<News> list, BmobException e) {
                if(e==null){
                    newslist=(ArrayList)list;
                    nadapter.addData(newslist);
                    nadapter.notifyDataSetChanged();
                }
                else {
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String url=nadapter.getItem(i).getUrl();
                System.out.println(url+"-------------------");
                Intent intent=new Intent(getActivity(), WebviewXYHActivity.class);
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });

        //实现页面滑动，下面圆点的变换
        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {

                for (int i = 0; i < pointlist.size(); i++) {
                    if (position == i) {
                        ImageView image = pointlist.get(i);
                        image.setBackgroundResource(R.mipmap.dot_d);
                    } else {
                        ImageView image = pointlist.get(i);
                        image.setBackgroundResource(R.mipmap.dot_c);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        Mythread mythread = new Mythread();
        mythread.start();




        return view;
    }
    private int i;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0)
                viewpage.setCurrentItem(0);
            else if (msg.what == 1)
                viewpage.setCurrentItem(1);
            else if (msg.what == 2)
                viewpage.setCurrentItem(2);
            else if (msg.what == 3)
                viewpage.setCurrentItem(3);
            else if (msg.what==4)
                viewpage.setCurrentItem(4);

        }
    };

    class Mythread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i <= 100; i++) {
                try {
                    sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i == 5) {
                    i = 0;
                }
                Message message = new Message();
                message.what = i;
                handler.sendMessage(message);
            }
        }
    }
}
